<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Room extends CI_Controller {
	protected  $language = 0;
	public function index($id=0)
	{
		$this->load->library('session');
		$this->load->library('parser');		
		$this->load->helper('language');
		$this->load->helper('url');
		$this->load->helper('xml'); 
		$data = array();
		$this->load->view('template/header',$data);
		$this->load->view('template/adventive',$data);
		$this->load->view('template/search',$data);
		$this->load->view('template/potsnews',$data);
		
		$this->load->model('news_model');
		$data['cateshow']=$this->news_model->cateshow(0);
		$data['catehide']=$this->news_model->catehide(0);
		$data['banner']=$this->news_model->banner();
		$data['newhot']=$this->news_model->newhot();
		$this->load->view('template/menuhome',$data);
		
		//$TotalRow= $this->news_model->gettotalproduct($parentcateid);
		///$Pagelink=$this->config->item("base_url")."index.php/cate/viewall/".$id."/".$all;
		//$rownum=40;
		$this->parser->parse('template/room', $data,false);
		
		$this->parser->parse('template/footer', $data,false);
	}

	function BuildPage($PageLink,$TotalRow,$CurrentPageIndex,$PageSize)
        {
		$TotalPage = 0;
		$MaxPage=10;
		$TotalPage = floor($TotalRow / $PageSize);
		if ($TotalRow % $PageSize > 0)
		{
			$TotalPage++;
		}
		if ($CurrentPageIndex > $TotalPage)
		{
			$CurrentPageIndex = $TotalPage;
		}
		if ($CurrentPageIndex < 1)
		{
			$CurrentPageIndex = 1;
		}
		$BeginRow=($CurrentPageIndex - 1) * $PageSize + 1;;
		$EndRow=($CurrentPageIndex - 1) * $PageSize + $PageSize;
		
		$arr=explode("?",$PageLink);
		$PageLink=$PageLink."/";
		if($TotalPage<2)
		{
			return "";
		}
		
		if ($TotalPage <=$MaxPage)
		{
			$BeginPage=1;
            }
		elseif ($CurrentPageIndex == 1)
		{
			$BeginPage=1;
		}
		elseif ($CurrentPageIndex == $TotalPage)
		{
			$BeginPage= $TotalPage + 1 - $MaxPage;
		}
		elseif ($CurrentPageIndex + $MaxPage / 2 >= $TotalPage)
		{
			$BeginPage= $TotalPage + 1 - $MaxPage;
		}
		elseif ($CurrentPageIndex - $MaxPage / 2 <= 1)
		{
			$BeginPage= 1;
		}
		else
		{
            $BeginPage=$CurrentPageIndex - $MaxPage / 2;
		}
		$Return="<div class=\"jb_pagination\">";
		if ($TotalPage > $MaxPage)
		{
			$Return=$Return . "<a href=\"" . (string)$PageLink . "1\">Đầu</a>";
			for ($i = 0; $i < $MaxPage; $i++)
			{
				if ($BeginPage + $i == $CurrentPageIndex)
				{
					$Return=$Return . "<a class=\"p_current\" href=\"" . (string)$PageLink . (string)($BeginPage + $i) . "\">" . (string)($BeginPage + $i) . "</a>";
				}
				else
				{
					$Return=$Return . "<a href=\"" . (string)$PageLink . (string)($BeginPage + $i) . "\">" . (string)($BeginPage + $i) . "</a>";
				}
			}
			$Return=$Return . "<a href=\"" . (string)$PageLink . (string)$TotalPage . "\">Cuối</a>";
		}
		else
		{
			for ($i = 0; $i < $TotalPage; $i++)
			{
				if ($BeginPage + $i == $CurrentPageIndex)
				{
					$Return=$Return . "<a class=\"active\" href=\"" . (string)$PageLink . (string)($BeginPage + $i) . "\">" . (string)($BeginPage + $i) . "</a>";
				}
				else
				{
					$Return=$Return . "<a href=\"" . (string)$PageLink . (string)($BeginPage + $i) . "\">" . (string)($BeginPage + $i) . "</a>";
				}
			}
		}

		$Return=$Return . "</div>";

		return $Return;
    }
}
